<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Menu as MenuModel;

class Menu extends Controller
{
    /**
     * 菜单列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $menus = MenuModel::all();
        return view('menu.menu',['menus'=>$menus]);
    }

    public function menuAdd()
    {
        return view('menu.add');
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
            'url' => 'required'
        ]);

        $menu = new MenuModel();
        $menu->name = $request->get('name');
        $menu->url = $request->get('url');
        if ($menu->save()){
            return redirect('menu');
        } else {
            return redirect()->back()->withInput()->withErrors('保存失败');
        }
    }
}
